{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "60f9a556",
   "metadata": {},
   "source": [
    "# week10周二版\n",
    "* 2022-10-25"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e0ec149",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "> 1. while循环：while True-->无限循环\n",
    ">> 1. 指定次数，while True 可以实现for循环的指定次数，增加一个count计数器即可\n",
    ">> 2. while True 一般与 用户输入 一起使用，让用户可以重复输入，知道某些条件结束输入（购物车）\n",
    "> 2. 购物车项目实践\n",
    "# 本周内容\n",
    "> 1. 元组\n",
    "> 2. 集合\n",
    "> 3. 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cecd8e4",
   "metadata": {},
   "source": [
    "# 1. 元组（tuple）\n",
    "> 1. 元组的基本内容（形态、定义）\n",
    "> 2. 元组的性质\n",
    ">> 1. 有序性（indexing，sliceing）\n",
    ">> 2. **不可变性** 元组没有增、删、改\n",
    "> 3. 元组的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "36e9802b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(180001, 180002, 180003) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网三_学号 = (180001,180002,180003)\n",
    "print(网三_学号,type(网三_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a410196c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(190001, 190002, 190003) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网二_学号 = 190001,190002,190003\n",
    "print(网二_学号,type(网二_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "adbe546f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "190001"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_学号[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3c4740e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(190002, 190003)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_学号[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "892f70c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=25, tm_hour=15, tm_min=7, tm_sec=8, tm_wday=1, tm_yday=298, tm_isdst=0)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组的实际案例\n",
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "be107d6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on struct_time object:\n",
      "\n",
      "class struct_time(builtins.tuple)\n",
      " |  struct_time(iterable=(), /)\n",
      " |  \n",
      " |  The time value as returned by gmtime(), localtime(), and strptime(), and\n",
      " |  accepted by asctime(), mktime() and strftime().  May be considered as a\n",
      " |  sequence of 9 integers.\n",
      " |  \n",
      " |  Note that several fields' values are not the same as those defined by\n",
      " |  the C language standard for struct tm.  For example, the value of the\n",
      " |  field tm_year is the actual year, not year - 1900.  See individual\n",
      " |  fields' descriptions for details.\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      struct_time\n",
      " |      builtins.tuple\n",
      " |      builtins.object\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      Helper for pickle.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  tm_gmtoff\n",
      " |      offset from UTC in seconds\n",
      " |  \n",
      " |  tm_hour\n",
      " |      hours, range [0, 23]\n",
      " |  \n",
      " |  tm_isdst\n",
      " |      1 if summer time is in effect, 0 if not, and -1 if unknown\n",
      " |  \n",
      " |  tm_mday\n",
      " |      day of month, range [1, 31]\n",
      " |  \n",
      " |  tm_min\n",
      " |      minutes, range [0, 59]\n",
      " |  \n",
      " |  tm_mon\n",
      " |      month of year, range [1, 12]\n",
      " |  \n",
      " |  tm_sec\n",
      " |      seconds, range [0, 61])\n",
      " |  \n",
      " |  tm_wday\n",
      " |      day of week, range [0, 6], Monday is 0\n",
      " |  \n",
      " |  tm_yday\n",
      " |      day of year, range [1, 366]\n",
      " |  \n",
      " |  tm_year\n",
      " |      year, for example, 1993\n",
      " |  \n",
      " |  tm_zone\n",
      " |      abbreviation of timezone name\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  n_fields = 11\n",
      " |  \n",
      " |  n_sequence_fields = 9\n",
      " |  \n",
      " |  n_unnamed_fields = 0\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      Return key in self.\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(self, key, /)\n",
      " |      Return self[key].\n",
      " |  \n",
      " |  __getnewargs__(self, /)\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __hash__(self, /)\n",
      " |      Return hash(self).\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __mul__(self, value, /)\n",
      " |      Return self*value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __rmul__(self, value, /)\n",
      " |      Return value*self.\n",
      " |  \n",
      " |  count(self, value, /)\n",
      " |      Return number of occurrences of value.\n",
      " |  \n",
      " |  index(self, value, start=0, stop=9223372036854775807, /)\n",
      " |      Return first index of value.\n",
      " |      \n",
      " |      Raises ValueError if the value is not present.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(time.localtime())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "3cdcec01",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4], [3, 4, 5, 6]) <class 'tuple'> 4366800576\n",
      "[1, 2, 3, 4] 4367451456\n"
     ]
    }
   ],
   "source": [
    "tuple_test = ([1,2,3,4],[3,4,5,6])\n",
    "print(tuple_test,type(tuple_test),id(tuple_test))\n",
    "print(tuple_test[0],id(tuple_test[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "e3549a51",
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple_text[0].append(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "fc032bab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([1, 2, 3, 4, 100, 100, 100, 100, 100, 10, 100], [3, 4, 5, 6])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple_text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "4e28238f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4], [3, 4, 5, 6]) <class 'tuple'> 4366800576\n",
      "[1, 2, 3, 4] 4367451456\n"
     ]
    }
   ],
   "source": [
    "print(tuple_test,type(tuple_test),id(tuple_test))\n",
    "print(tuple_test[0],id(tuple_test[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d5cdb59",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一般情况下，元组里面存放的是 常量（不可变），何为常量：数字、字符串\n",
    "#                 不存放变量，如：列表、字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cd54c95",
   "metadata": {},
   "source": [
    "# 2.集合（sets）\n",
    "> 1. 集合的基本概念\n",
    ">> 1. 性质：没有重复项\n",
    ">> 2. 性质：无序性\n",
    "> 2. 集合的一般方法\n",
    ">> 1. 集合的运算：& ，|\n",
    ">> 2. 集合的一般方法：intersection,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "3b8937d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'orange', 'apple', 'pear', 'orange', 'banana']\n",
      "{'orange', 'pear', 'apple', 'banana'}\n"
     ]
    }
   ],
   "source": [
    "basket_lists = ['apple','orange','apple','pear','orange','banana']\n",
    "basket_sets = {'apple','orange','apple','pear','orange','banana'}\n",
    "print(basket_lists)\n",
    "print(basket_sets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "03c88d04",
   "metadata": {},
   "outputs": [],
   "source": [
    "舞蹈社 = {1801,1802,1803,1804,1805}\n",
    "电竞社 = {1801,1802,1903,1904,1905}"
   ]
  },
  {
   "cell_type": "raw",
   "id": "2472b986",
   "metadata": {},
   "source": [
    "· 集合的与运算，交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "3f8bd303",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社 & 电竞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "12735246",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社.intersection(电竞社)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "3a701304",
   "metadata": {},
   "source": [
    "集合的或运算，并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "b233e7d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1804, 1805, 1903, 1904, 1905}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社 | 电竞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "9d0d4b2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(舞蹈社 | 电竞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "fa73ee11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1804, 1805, 1903, 1904, 1905}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社.union(电竞社)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cae16f72",
   "metadata": {},
   "source": [
    "# 3.字典（dict）\n",
    "\n",
    "> 1. 字典的基本概念：\n",
    ">> 1. 字典性质：\n",
    ">>> 1. 无序性\n",
    ">>> 2. keys 是不可变的:就像列表、字符串等有序的数据结构他的index不可变是一样的。\n",
    ">>> 3. keys是唯一的\n",
    ">> 2. 字典的形态\n",
    "> 2. 字典的一般方法\n",
    ">> 1. 字典的创建\n",
    ">> 2. 字典的函数和方法\n",
    ">>> 1. 增加\n",
    ">>> 2. 删除\n",
    ">>> 3. 修改"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08fdef12",
   "metadata": {},
   "source": [
    "## 3.1 字典的基本概念"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "c4ca4a6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新二班 = {\n",
    "    180001:\"eason\",\n",
    "    180002:\"jay\",\n",
    "    180003:\"李荣浩\",\n",
    "    180004:\"薛之谦\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "ff7ec233",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{180001: 'eason', 180002: 'jay', 180003: '李荣浩', 180004: '薛之谦'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(网新二班,type(网新二班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d24ece6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 因为学号是唯一的，可以作为字典的key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "6a8ac757",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{180001: 'eason', 180002: 'jay', 180003: '李荣浩', 180004: '薛之谦', 180005: '李荣浩'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "网新三班 = {\n",
    "    180001:\"eason\",\n",
    "    180002:\"jay\",\n",
    "    180003:\"李荣浩\",\n",
    "    180004:\"薛之谦\",\n",
    "    180005:\"李荣浩\"\n",
    "}\n",
    "print(网新三班,type(网新三班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "c7e599f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'jay'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典的取值：通过keys 进行字典的取值，unlike列表、元组和字符串通过index取值\n",
    "网新三班[180002]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "6c12ee6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新三班 = {\n",
    "    180001:[\"eason\",\"香港\",46,\"聊天记录\",\"朋友圈\"],\n",
    "    180002:\"jay\",\n",
    "    180003:\"李荣浩\",\n",
    "    180004:\"薛之谦\",\n",
    "    180005:\"李荣浩\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "8cb4d00d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['eason', '香港', 46, '聊天记录', '朋友圈']"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新三班[180001]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c9a85d4",
   "metadata": {},
   "source": [
    "## 3.2.字典的一般方法\n",
    "* 1.按照字典的形式直接创建（key:value）\n",
    "……\n",
    "网新二班 = {\n",
    "    180001:\"eason\",\n",
    "    180002:\"jay\",\n",
    "    180003:\"李荣浩\",\n",
    "    180004:\"薛之谦\"\n",
    "}\n",
    "……\n",
    "* 2.使用dict( )方法创建字典\n",
    "* 3.使用字典增加的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "3cda9203",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用dict（ ）方法创建字典\n",
    "网新二班 = dict(\n",
    "    stu180001=\"eason\",\n",
    "    stu180002=\"jay\",\n",
    "    stu180003=\"李荣浩\",\n",
    "    stu180004=\"薛之谦\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "a04bd72f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu180001': 'eason',\n",
       " 'stu180002': 'jay',\n",
       " 'stu180003': '李荣浩',\n",
       " 'stu180004': '薛之谦'}"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新二班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "42fad505",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用字典增加的方式创建字典：dict[key] = value\n",
    "网新一班 = {}\n",
    "网新一班[\"stu180001\"] = 'eason'\n",
    "网新一班[\"stu180002\"] = 'jay'\n",
    "网新一班[\"stu180003\"] = '李荣浩'\n",
    "网新一班[\"stu180004\"] = '薛之谦'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "78a003cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu180001': 'eason',\n",
       " 'stu180002': 'jay',\n",
       " 'stu180003': '李荣浩',\n",
       " 'stu180004': '薛之谦'}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b36a358",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新一班_keys = [\"stu1800001\",\"stu1800002\",\"stu1800003\",\"stu1800004\"]\n",
    "网新一班_values = ['李一峰','Jack','Marry','周杰伦']\n",
    "网新一班 = {}\n",
    "for i in range(4):\n",
    "    网新一班[网新一班_keys[i]] = 网新一班_values[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "e1c63946",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu180001': 'eason',\n",
       " 'stu180002': 'jay',\n",
       " 'stu180003': '李荣浩',\n",
       " 'stu180004': '薛之谦'}"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "691b8d72",
   "metadata": {},
   "source": [
    "课后：1.字典的课堂笔记练习；2.字典的方法预习"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
